package org.zjt.mock.demo.mock.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.connector.RequestFacade;
import org.springframework.http.MediaType;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.accept.ContentNegotiationStrategy;
import org.springframework.web.context.request.NativeWebRequest;

import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * Description:  response 返回这是content-type
 *
 * @author juntao.zhang
 * Date: 2018-06-21 下午4:59
 * @see ContentNegotiationStrategy
 */

@Slf4j
public class CustomizedContentNegotiationStrategy implements ContentNegotiationStrategy {
    @Override
    public List<MediaType> resolveMediaTypes(NativeWebRequest nativeWebRequest) throws HttpMediaTypeNotAcceptableException {
        RequestFacade requestFacade = nativeWebRequest.getNativeRequest(RequestFacade.class);
        boolean contains = requestFacade.getRequestURI().indexOf("user3") > -1;

        contains = new Random().nextInt(2) % 2 == 0 ;
        log.info("contains:{}",contains);

        if (contains){
            return Arrays.asList(MediaType.ALL);
        }

        return Arrays.asList(MediaType.ALL);
    }

}
